import { sysGetUserList,sysAddUser,sysUpdateUser,getRolesByCreateUser,newsysgetRolesByCreateUser,sysDelUser,getRoleByStatusOrCreateBy } from '@/services/overviewRobots';

export default {
  namespace: 'userManagement',

  state: {
   
  },
  effects: {
    *sysGetUserList({ payload, callback }, { call, put }) { // 获取用户列表
      const response = yield call(sysGetUserList, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *sysAddUser({ payload, callback }, { call, put }) { // 添加用户
      const response = yield call(sysAddUser, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *sysUpdateUser({ payload, callback }, { call, put }) { // 编辑用户
      const response = yield call(sysUpdateUser, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *getRolesByCreateUser({ payload, callback }, { call, put }) { // 通过用户的id获取角色列表
      const response = yield call(getRolesByCreateUser, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *newsysgetRolesByCreateUser({ payload, callback }, { call, put }) { // 通过用户的id获取角色列表
      const response = yield call(getRoleByStatusOrCreateBy, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
    *sysDelUser({ payload, callback }, { call, put }) { // 删除用户
      const response = yield call(sysDelUser, payload);
      if (response) {
        if (callback && typeof callback === 'function') {
          callback(response);
        }
        yield put({
          type: 'save',
          payload: response,
        });
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
    fetchb(state,{payload}){
      return {
        ...state,
        ...payload,
      };
    },
    clear() {
      return {
        total:'',
        list:[]
      };
    },
  },
};
